package com.example.wan_android.ui.activity

import android.util.Log
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.wan_android.BR
import com.example.wan_android.R
import com.example.wan_android.adapter.KnowledgeBodyAdapter
import com.example.wan_android.databinding.ActivityKnowledgeBodyBinding
import com.example.wan_android.tools.SpacesItemDecoration
import com.example.wan_android.viewmodel.KnowledgeBodyViewModel

class KnowledgeBodyActivity : BaseActivity<ActivityKnowledgeBodyBinding, KnowledgeBodyViewModel>(
    KnowledgeBodyViewModel::class.java,
    R.layout.activity_knowledge_body,
    BR.viewModel
) {
    private val adapter by lazy {
        KnowledgeBodyAdapter(mViewModel!!)
    }

    override fun initView() {
        mViewModel?.isSuccess?.observe(this) {
            if (it) {
                mViewDataBinding?.ivEmpty?.visibility = View.GONE
                mViewDataBinding?.rvKnowledgeBody?.visibility = View.VISIBLE
            } else {
                mViewDataBinding?.ivEmpty?.visibility = View.VISIBLE
                mViewDataBinding?.rvKnowledgeBody?.visibility = View.GONE
            }
        }
        val title = intent.getStringExtra(KnowledgeBodyViewModel.title)
        val id = intent.getIntExtra(KnowledgeBodyViewModel.id, 0)
        val index = intent.getIntExtra(KnowledgeBodyViewModel.index, 0)
        mViewModel?.mTitle = title ?: ""
        mViewModel?.mId = id
        mViewModel?.mIndex = index
        mViewModel?.getKnowledgeBodyData()
        mViewDataBinding?.rvKnowledgeBody?.adapter = adapter
        mViewDataBinding?.rvKnowledgeBody?.layoutManager =
            LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
        mViewDataBinding?.rvKnowledgeBody?.addItemDecoration(SpacesItemDecoration(40))
        mViewModel?.knowledgeBodyData?.observe(this) {
            it.onSuccess {
                adapter.setListData(it)
            }
            it.onFailure {
            }
        }
    }
}